package com.example.test.Controller;


import com.example.test.Bean.UserBean;
import com.example.test.Bean.UserImgBean;
import com.example.test.servicelmpl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Resource
    UserServiceImpl userService;

    @RequestMapping("/users")
    public String showUsers(ModelMap modelMap) {
        List<UserBean> userList = userService.getAllUser();
        modelMap.addAttribute(" userList", userList);
        return "users";
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Map addUser(UserBean userBean) {
        int flag = userService.insertUser(userBean);
        Map<String, Object> map = new HashMap<String, Object>();
        if (flag == 1) {
            map.put("msg", "新增用户成功");
            return map;
        } else {
            map.put("msg", "新增用户失败");
            return map;
        }
    }

    public String deleteUser(@PathVariable("id") String id, ModelMap
            modelMap) {
        int flag = userService.deleteUser(id);
        List<UserBean> userList = userService.getAllUser();
        modelMap.addAttribute("userList", userList);
        if (flag == 1) {
            return "users";
        } else {
            return "error";
        }
    }

    @RequestMapping("/user+ {id}")
    public String getUserById(@PathVariable("id") String id, ModelMap modelMap) {
        UserBean userBean = userService.getUserById(id);
        List<UserImgBean> listUserImgBean = userService.getUserImgsById(id);
        modelMap.addAttribute("user", userBean);
        modelMap.addAttribute(" userImgsList" ,listUserImgBean);
        return "userInfo";
    }

    @RequestMapping("/users")
    public String index(Model model, @RequestParam(defaultValue = "2")
            Integer pageNum, @RequestParam(defaultValue = "3") Integer
                                pageSize) {
        PageInfo<UserBean> pageInfo = userService.selectUser4Page(pageNum, pageSize);
//4.使用model传参数回前端
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("userList", pageInfo.getList());
        return "users";
    }


    @RequestMapping("/modifyUser")
    @ResponseBody
    public Map updateUser(UserBean userBean) {
        int flag = userService.updateUser(userBean);
        Map<String, Object> map = new HashMap<>();
        if (flag == 1) {
            map.put("msg", "修改用户信息成功");
            return map;
        } else {
            map.put("msg", "修改用户信息失败");
            return map;
        }
    }
}
